package com.haixiaoke.saas.common.utils;

import cn.hutool.core.convert.Convert;

import java.util.HashMap;
import java.util.Map;

/**
 * 占位符 Map 工具类
 */
public class PlaceholderUtil {

    /**
     * Format for system property placeholders: "${}"
     */
    public static final String PLACEHOLDER_FORMAT_DOLLAR_SUPPORT = "1";

    /**
     * Format for system property placeholders: "{}"
     */
    public static final String PLACEHOLDER_FORMAT_SUPPORT = "2";

    /**
     * Prefix for system property placeholders: "${"
     */
    public static final String PLACEHOLDER_PREFIX = "${";
    /**
     * Suffix for system property placeholders: "}"
     */
    public static final String PLACEHOLDER_SUFFIX = "}";

    /**
     * Prefix for system property placeholders: "{"
     */
    public static final String PLACEHOLDER_PREFIX_NAME = "{";
    /**
     * Suffix for system property placeholders: "}"
     */
    public static final String PLACEHOLDER_SUFFIX_NAME = "}";


    public static String format(String text, Map<String, Object> parameter, String placeholderFormat) {
        String placeholderPrefix = PLACEHOLDER_PREFIX;
        String placeholderSuffix = PLACEHOLDER_SUFFIX;
        if (placeholderFormat.equals(PLACEHOLDER_FORMAT_SUPPORT)){
            placeholderPrefix = PLACEHOLDER_PREFIX_NAME;
            placeholderSuffix = PLACEHOLDER_SUFFIX_NAME;
        }
        if (parameter == null || parameter.isEmpty()) {
            return text;
        }
        StringBuffer buf = new StringBuffer(text);
        int startIndex = buf.indexOf(placeholderPrefix);
        while (startIndex != -1) {
            int endIndex = buf.indexOf(placeholderSuffix, startIndex + placeholderPrefix.length());
            if (endIndex != -1) {
                String placeholder = buf.substring(startIndex + placeholderPrefix.length(), endIndex);
                int nextIndex = endIndex + placeholderSuffix.length();
                try {
                    String propVal = Convert.toStr(parameter.get(placeholder));
                    if (propVal != null) {
                        buf.replace(startIndex, endIndex + placeholderSuffix.length(), propVal);
                        nextIndex = startIndex + propVal.length();
                    } else {
                        System.out.println("Could not resolve placeholder '" + placeholder + "' in [" + text + "] ");
                    }
                } catch (Exception ex) {
                    System.out.println("Could not resolve placeholder '" + placeholder + "' in [" + text + "]: " + ex);
                }
                startIndex = buf.indexOf(placeholderPrefix, nextIndex);
            } else {
                startIndex = -1;
            }
        }
        return buf.toString();
    }

    public static void main(String[] args) {
        String template = "${name}${gender}，您好！今天是您的生日，${bankName}银行祝您生日快乐，愿您今天的回忆温馨，愿您今天的梦想甜美，愿您这一年称心如意。";
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("name", "志平");
        parameters.put("gender", "先生");
        parameters.put("bankName", "中国");
        System.out.println(PlaceholderUtil.format(template, parameters, ""));
    }
}

